<?php


namespace app\teacher\controller;

use think\Controller;
use app\teacher\model\TisType as TisTypeModel;
use app\teacher\model\TisSource as TisSourceModel;
use app\teacher\model\AddThesis as AddThesisModel;
use app\teacher\security\TchCheck as TchCheckLogic;
use app\teacher\security\Auth as AuthLogic;
use think\Loader;
use think\Session;

class AddThesis extends Controller
{
    /**
     * 添加题目页面跳转
     * @access public
     * @return mixed 返回登录视图
     */
    public function addThesis()
    {
        $tchCheck = new TchCheckLogic();
        //检测教师登录情况
        if ($tchCheck->check() == 1) {
            //检测是否有添加题目权限
            $auth = new AuthLogic();
            if ($auth->check('add', Session::get('token')['tchUsername'])) {
                $config = getSrvConfig();
                if ($config) {
                    if ($config['srvStatus'] == 1) {
                        //实例化题目来源模型
                        $tisSource = new TisSourceModel();
                        //实例化题目类型模型
                        $tisType = new TisTypeModel();
                        //查询来源、类型列表
                        $sourceList = $tisSource::all();
                        $typeList = $tisType::all();
                        if ($sourceList && $typeList) {
                            $this->assign('sourceList', $sourceList);
                            $this->assign('typeList', $typeList);
                        } else {
                            //如果读取失败，使用默认选项
                            $sourceList = [6 => '其他',];
                            $typeList = [7 => '其他'];
                            $this->assign('sourceList', $sourceList);
                            $this->assign('typeList', $typeList);
                        }
                    }else{
                        return "现在不是申报阶段";
                    }
                }else{
                    return "配置查询出错，请稍后再试";
                }
            } else {
                return "错误500：您没有添加题目的权限";
            }
        } else {
            return "错误600：您的登录信息出现问题，请重新登录"
            . "<br>" . "1.您的帐号在其他地方登录"
            . "<br>" . "2.您在线时间过长（5小时以上）"
            . "<br>" . "3.令牌验证错误";
        }

        return $this->fetch('addThesis/addThesis');
    }

    /**
     * 添加新题目功能
     * @access public
     * @param string $title 题目名称
     * @param string $source 题目来源
     * @param string $type 题目类型
     * @param string $main 题目介绍
     * @param string $ask 对学生要求
     * @return integer 返回执行代码
     */
    public function do_addThesis($title = '', $source = '', $type = '', $main = '', $ask = '')
    {
        //检测是否在申报阶段
        $config = getSrvConfig();
        if ($config) {
            if ($config['srvStatus'] == 1) {
                $tchCheck = new TchCheckLogic();
                //检测教师登录情况
                if ($tchCheck->check()) {
                    //检测是否有添加题目权限
                    $auth = new AuthLogic();
                    //获取教师工号
                    $username = Session::get('token')['tchUsername'];
                    if ($auth->check('add', $username)) {
                        $data = [
                            'tisTitle' => htmlspecialchars($title),
                            'tisSource' => htmlspecialchars($source),
                            'tisType' => htmlspecialchars($type),
                            'tisMain' => htmlspecialchars($main),
                            'tisAsk' => htmlspecialchars($ask),
                            'tisAuthor' => htmlspecialchars($username),
                        ];
                        $validate = Loader::validate('Thesis');
                        //输入参数是否正确
                        if ($validate->check($data)) {
                            //实例化题目模型类
                            $thesis = new AddThesisModel();
                            $tisNum=$thesis::where('tisAuthor',$username)->count();
                            //提交数量超过限制
                            if($tisNum<$config['tisNum']){
                                if ($thesis->save($data)) {
                                    return 1;
                                } else {
                                    return "题目写入失败，请稍后再试";
                                }
                            }else{
                                return "最多只能提交".$config['tisNum']."个题目";
                            }
                        } else {
                            return $validate->getError();
                        }
                    } else {
                        return "500：您没有添加题目的权限";
                    }
                } else {
                    return "600：您的登录信息出现问题，请重新登录"
                    . "<br>" . "1.您的帐号在其他地方登录"
                    . "<br>" . "2.您在线时间过长（5小时以上）"
                    . "<br>" . "3.令牌验证错误";
                }
            } else {
                return "现在不是申报阶段，请不要提交题目";
            }
        } else {
            return "配置查询出错，请稍后再试";
        }
    }
}